
using System;
using System.Text;
using SpotifyGenNHibernate.CEN;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Criterion;
using NHibernate.Exceptions;
using SpotifyGenNHibernate.EN.Spotify;

namespace SpotifyGenNHibernate.CAD.Spotify
{
        public partial class RadioCAD : BasicCAD, IRadioCAD
        {
        public RadioCAD() : base ()
        {
        }

        public RadioCAD(ISession sessionAux) : base (sessionAux)
        {
        }



        public RadioEN ReadOIDDefault (String IdRadio)
        {
                RadioEN radioEN = null;

                try
                {
                        SesssionInitializeTransaction ();
                        radioEN = (RadioEN)session.Load (typeof(RadioEN),
                                IdRadio

                                );
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in RadioCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return radioEN;
        }


        public String CrearRadio (RadioEN radio)
        {
                try
                {
                        SesssionInitializeTransaction ();
                        if (radio.Artista != null) {
                                for (int i = 0; i < radio.Artista.Count; i++)
                                        radio.Artista [i] = (SpotifyGenNHibernate.EN.Spotify.ArtistaEN)session.Load (typeof(SpotifyGenNHibernate.EN.Spotify.ArtistaEN), radio.Artista [i].IdArtista);
                        }

                        session.Save (radio);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in RadioCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return radio.IdRadio;
        }

        public void BorrarRadio (String IdRadio)
        {
                try
                {
                        SesssionInitializeTransaction ();
                        RadioEN radioEN = (RadioEN)session.Load (typeof(RadioEN),
                                IdRadio

                                );
                        session.Delete (radioEN);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in RadioCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }
        }

        public RadioEN ObtenerRadio (String IdRadio)
        {
                RadioEN radioEN = null;

                try
                {
                        SesssionInitializeTransaction ();
                        radioEN = (RadioEN)session.Load (typeof(RadioEN),
                                IdRadio

                                );
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in RadioCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return radioEN;
        }

        public void AñadirArtista (String p_Radio_OID, System.Collections.Generic.IList<int> p_artista_OIDs)
        {
                SpotifyGenNHibernate.EN.Spotify.RadioEN radioEN = null;
                try
                {
                        SesssionInitializeTransaction ();
                        radioEN = (RadioEN)session.Load (typeof(RadioEN), p_Radio_OID);
                        SpotifyGenNHibernate.EN.Spotify.ArtistaEN artistaEN = null;
                        if (radioEN.Artista == null) {
                                radioEN.Artista = new System.Collections.Generic.List<EN.Spotify.ArtistaEN>();
                        }

                        foreach (int item in p_artista_OIDs) {
                                artistaEN = new EN.Spotify.ArtistaEN ();
                                artistaEN = (EN.Spotify.ArtistaEN)session.Load (typeof(EN.Spotify.ArtistaEN), item);
                                radioEN.Artista.Add (artistaEN);
                        }


                        session.Update (radioEN);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in RadioCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }
        }

        public void EliminarArtista (String p_Radio_OID, System.Collections.Generic.IList<int> p_artista_OIDs)
        {
                try
                {
                        SesssionInitializeTransaction ();
                        SpotifyGenNHibernate.EN.Spotify.RadioEN radioEN = null;
                        radioEN = (RadioEN)session.Load (typeof(RadioEN), p_Radio_OID);

                        EN.Spotify.ArtistaEN artistaEN = null;
                        if (radioEN.Artista != null) {
                                foreach (int item in p_artista_OIDs) {
                                        artistaEN = (ArtistaEN)session.Load (typeof(ArtistaEN), item);
                                        if (radioEN.Artista.Contains (artistaEN) == true)
                                                radioEN.Artista.Remove (artistaEN);
                                        else
                                                throw new ModelException ("The identifier " + item + " in p_artista_OIDs you are trying to unrelationer, doesn't exist in RadioEN");
                                }
                        }

                        session.Update (radioEN);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in RadioCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }
        }
        }
}
